﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace RYTools
{
    public class GetIP
    {
        public static string GetHostIP()
        {
            try
            {
                string ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault<IPAddress>(a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString();//IP地址
                return ip;
            }
            catch (Exception ex)
            {
                throw new Exception("获取本机IP异常", ex);
            }
        }
        /// <summary>
        /// 获取本机ip列表
        /// </summary>
        /// <returns></returns>
        public static List<string> GetHostIPList()
        {
            try
            {
                string hostname = Dns.GetHostName();
                List<string> ipList = new List<string>();
                System.Net.IPHostEntry ipEntry = Dns.GetHostEntry(hostname);

                foreach (IPAddress ip in ipEntry.AddressList)
                {
                    if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        ipList.Add(ip.ToString());
                    }
                }

                return ipList;
            }
            catch (Exception ex)
            {
                throw new Exception("获取本机IP异常", ex);
            }
        }
     

        [System.Runtime.InteropServices.DllImport("Iphlpapi.dll")]
        static extern int SendARP(Int32 DestIP, Int32 SrcIP, ref Int64 MacAddr, ref Int32 PhyAddrLen);
        /// <summary> 
        /// SendArp获取MAC地址 
        /// </summary> 
        /// <returns></returns> 
        public static string GetMacAddressBySendARP()
        {
            StringBuilder strReturn = new StringBuilder();
            try
            {
                System.Net.IPHostEntry Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(Dns.GetHostName());
                System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
                Int32 remote = (int)TempAd[0].Address;
                Int64 macinfo = new Int64();
                Int32 length = 6;
                SendARP(remote, 0, ref macinfo, ref length);

                string temp = System.Convert.ToString(macinfo, 16).PadLeft(12, '0').ToUpper();

                int x = 12;
                for (int i = 0; i < 6; i++)
                {
                    if (i == 5) { strReturn.Append(temp.Substring(x - 2, 2)); }
                    else { strReturn.Append(temp.Substring(x - 2, 2) + ":"); }
                    x -= 2;
                }
                return strReturn.ToString();
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 获取本地计算机的名称
        /// </summary>
        /// <returns></returns>
        public static string GetHostName()
        {
            return Dns.GetHostName();
        }
        /// <summary>
        /// 获取mac地址第二种方法
        /// </summary>
        /// <returns></returns>
        public static List<string> GetMacByNetworkInterface()
        {
            List<string> macs = new List<string>();
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces)
            {
                macs.Add(ni.GetPhysicalAddress().ToString());
            }
            return macs;
        }
    }
}
